{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [2 3 4]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "A = np.array([[1,2,3],\n",
    "              [2,3,4]])\n",
    "print(A)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number of dim: 2\n"
     ]
    }
   ],
   "source": [
    "print('number of dim:',A.ndim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "shape: (2, 3)\n"
     ]
    }
   ],
   "source": [
    "print('shape:',A.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "size: 6\n"
     ]
    }
   ],
   "source": [
    "print('size:',A.size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2.   2.3  4. ]\n",
      " [ 2.   3.2  4. ]]\n",
      "[[ 0.  0.  0.  0.]\n",
      " [ 0.  0.  0.  0.]\n",
      " [ 0.  0.  0.  0.]]\n",
      "[[1 1 1]\n",
      " [1 1 1]]\n",
      "[[ 0.  0.  0.  0.]\n",
      " [ 0.  0.  0.  0.]\n",
      " [ 0.  0.  0.  0.]]\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "[[  1.    2.8   4.6]\n",
      " [  6.4   8.2  10. ]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "A = np.array([[2,2.3,4],\n",
    "             [2,3.2,4]])\n",
    "A0 = np.zeros((3,4))\n",
    "A1 = np.ones((2,3),dtype=np.int16)\n",
    "Amin = np.empty((3,4))\n",
    "Arange = np.arange(12).reshape((3,4))\n",
    "Aline = np.linspace(1,10,6).reshape((2,3))\n",
    "print(A)\n",
    "print(A0)\n",
    "print(A1)\n",
    "print(Amin)\n",
    "print(Arange)\n",
    "print(Aline)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  0.  0.  0.]\n",
      " [ 0.  0.  0.  0.]\n",
      " [ 0.  0.  0.  0.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "Amin = np.empty((3,4))\n",
    "print(Amin)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 20 30 40] [0 1 2 3]\n",
      "[False False  True  True] [ True  True  True False]\n",
      "[10 19 28 37]\n",
      "[10 21 32 43]\n",
      "[  0  20  60 120]\n",
      "[0 1 4 9]\n",
      "[-5.44021111  9.12945251 -9.88031624  7.4511316 ]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([10,20,30,40])\n",
    "b = np.arange(4)\n",
    "print(a,b)\n",
    "print(a>20,b<3)\n",
    "c = a-b\n",
    "d = a+b\n",
    "e = a*b\n",
    "f = b**2\n",
    "g = 10*np.sin(a)#cos tan等\n",
    "\n",
    "print(c)\n",
    "print(d)\n",
    "print(e)\n",
    "print(f)\n",
    "print(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1]\n",
      " [0 1]]\n",
      "[[0 1]\n",
      " [2 3]]\n",
      "[[0 1]\n",
      " [0 3]]\n",
      "[[2 4]\n",
      " [2 3]]\n",
      "[[2 4]\n",
      " [2 3]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([[1,1],\n",
    "             [0,1]])\n",
    "b = np.arange(4).reshape(2,2)\n",
    "print(a)\n",
    "print(b)\n",
    "c = a*b\n",
    "c_dot = np.dot(a,b)\n",
    "c_dot_1 = a.dot(b)\n",
    "print(c)\n",
    "print(c_dot)\n",
    "print(c_dot_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.2706944   0.48027564  0.00835308  0.83170055]\n",
      " [ 0.31429675  0.54158937  0.87013878  0.25754769]]\n",
      "3.57459626981\n",
      "0.870138784946\n",
      "0.00835308363163\n",
      "[ 1.59102368  1.98357259]\n",
      "[ 0.2706944   0.48027564  0.00835308  0.25754769]\n",
      "[ 0.00835308  0.25754769]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.random.random((2,4))\n",
    "print(a)\n",
    "print(np.sum(a))\n",
    "print(np.max(a))\n",
    "print(np.min(a))\n",
    "print(np.sum(a,axis=1))\n",
    "print(np.min(a,axis=0))\n",
    "print(np.min(a,axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]]\n",
      "0\n",
      "11\n",
      "7.5\n",
      "7.5\n",
      "[ 6.  7.  8.  9.]\n",
      "7.5\n",
      "[ 2  5  9 14 20 27 35 44 54 65 77 90]\n",
      "[[1 1 1]\n",
      " [1 1 1]\n",
      " [1 1 1]]\n",
      "[[ 2  6 10]\n",
      " [ 3  7 11]\n",
      " [ 4  8 12]\n",
      " [ 5  9 13]]\n",
      "[[ 2  6 10]\n",
      " [ 3  7 11]\n",
      " [ 4  8 12]\n",
      " [ 5  9 13]]\n",
      "[[5 5 5 5]\n",
      " [6 7 8 9]\n",
      " [9 9 9 9]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.arange(2,14).reshape((3,4))\n",
    "\n",
    "print(a)\n",
    "print(np.argmin(a))#最小索引值\n",
    "print(np.argmax(a))\n",
    "print(np.mean(a))#与下一行代码相同\n",
    "print(a.mean())\n",
    "print(np.mean(a,axis=0))\n",
    "print(np.median(a))#中位数\n",
    "print(np.cumsum(a))#第i个数是前i个数相加\n",
    "print(np.diff(a))#累差，i=i+1-i\n",
    "#print(np.nonzero(a))\n",
    "print(np.transpose(a))#矩阵的转置，行变列,同下\n",
    "print(a.T)\n",
    "print(np.clip(a,5,9))#阈值控制，大于某值就为某值，小于类似"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3  4  5  6  7  8  9 10 11 12 13 14]\n",
      "6\n",
      "[[ 3  4  5  6]\n",
      " [ 7  8  9 10]\n",
      " [11 12 13 14]]\n",
      "[11 12 13 14]\n",
      "8\n",
      "8\n",
      "[ 7  8  9 10]\n",
      "[ 4  8 12]\n",
      "[8 9]\n",
      "[3 4 5 6]\n",
      "[ 7  8  9 10]\n",
      "[11 12 13 14]\n",
      "[ 3  7 11]\n",
      "[ 4  8 12]\n",
      "[ 5  9 13]\n",
      "[ 6 10 14]\n",
      "[ 3  4  5  6  7  8  9 10 11 12 13 14]\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n",
      "12\n",
      "13\n",
      "14\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.arange(3,15)\n",
    "print(a)\n",
    "print(a[3])#索引\n",
    "b = np.arange(3,15).reshape(3,4)\n",
    "print(b)\n",
    "print(b[2])\n",
    "print(b[1][1])\n",
    "print(b[1,1])\n",
    "print(b[1,:])\n",
    "print(b[:,1])\n",
    "print(b[1,1:3])\n",
    "for row in b:\n",
    "    print(row)\n",
    "for col in b.T:\n",
    "    print(col)\n",
    "print(b.flatten())\n",
    "for item in b.flat:\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 3  4  5  6]\n",
      " [ 7  8  9 10]\n",
      " [11 12 13 14]]\n",
      "[8 9]\n",
      "[ 3  4  5  6  7  8  9 10 11 12 13 14]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.arange(3,15).reshape((3,4))\n",
    "print(a)\n",
    "print(a[1,1:3])\n",
    "print(a.flatten())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3,) (3,)\n",
      "(2, 3)\n",
      "[1 1 1 2 2 2]\n",
      "(6,)\n",
      "[[1 1 1]]\n",
      "[[1]\n",
      " [1]\n",
      " [1]]\n",
      "[[1]\n",
      " [1]\n",
      " [1]\n",
      " [2]\n",
      " [2]\n",
      " [2]]\n",
      "[[1 2]\n",
      " [1 2]\n",
      " [1 2]]\n",
      "[[1 2 2 1]\n",
      " [1 2 2 1]\n",
      " [1 2 2 1]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([1,1,1])\n",
    "b = np.array([2,2,2])\n",
    "c = np.vstack((a,b))#上下合并\n",
    "d = np.hstack((a,b))#左右合并\n",
    "print(a.shape,b.shape)\n",
    "print(c.shape)  \n",
    "print(d)\n",
    "print(d.shape)\n",
    "print(a[np.newaxis,:])\n",
    "print(a[:,np.newaxis])\n",
    "A = a[:,np.newaxis]\n",
    "B = b[:,np.newaxis]\n",
    "e = np.vstack((A,B))\n",
    "f = np.hstack((A,B))\n",
    "print(e)\n",
    "print(f)\n",
    "C = np.concatenate((A,B,B,A),axis=1)\n",
    "print(C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1]\n",
      " [1]\n",
      " [1]\n",
      " [2]\n",
      " [2]\n",
      " [2]]\n"
     ]
    }
   ],
   "source": [
    "print(e)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "[array([[0, 1],\n",
      "       [4, 5],\n",
      "       [8, 9]]), array([[ 2,  3],\n",
      "       [ 6,  7],\n",
      "       [10, 11]])]\n",
      "[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]\n",
      "[array([[0, 1],\n",
      "       [4, 5],\n",
      "       [8, 9]]), array([[ 2],\n",
      "       [ 6],\n",
      "       [10]]), array([[ 3],\n",
      "       [ 7],\n",
      "       [11]])]\n",
      "[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]\n",
      "[array([[0, 1],\n",
      "       [4, 5],\n",
      "       [8, 9]]), array([[ 2,  3],\n",
      "       [ 6,  7],\n",
      "       [10, 11]])]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.arange(12).reshape((3,4))\n",
    "print(a)\n",
    "print(np.split(a,2,axis=1))#1代表列<1>\n",
    "print(np.split(a,3,axis=0))#<2>\n",
    "print(np.array_split(a,3,axis=1))#<3>\n",
    "print(np.vsplit(a,3))#<4>\n",
    "print(np.hsplit(a,2))#<5>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3]\n",
      "[1 1 2 3]\n",
      "[1 1 2 3]\n",
      "[1 1 2 3]\n",
      "[1 1 2 0]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.arange(4)\n",
    "b = a\n",
    "c = b\n",
    "print(a)\n",
    "b[0] = 1\n",
    "print(a)\n",
    "d = a.copy()\n",
    "print(d)\n",
    "c[3] = 0\n",
    "print(d)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
